//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    img:{
      url:"http://ones.cakeyouxuan.com/public/white_bg.png"
    }
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  saveImgToPhotosAlbumTap: function () {
    wx.downloadFile({
      url: "http://ones.cakeyouxuan.com/public/white_bg.png",
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
            console.log('fail **saveimage')
          }
        })
      },
      fail: function () {
        console.log('fail **download')
      }
    })
  },
  previewImg: function () {
    this.setData({
      previewImage: "block"
    });
  },
  saveImg: function () {
    longTouch = true;
    this.setData({
      saveImage: false
    })
  },
  closePrev: function () {
    if (longTouch) {
      longTouch = false;
    } else {
      this.setData({
        previewImage: "none"
      });
    }
  },
  actionSheetChange: function () {
    this.setData({
      saveImage: !this.data.saveImage
    })
  },
  downloadFile: function (e) {
    console.log(e);
    var imgUrl = e.currentTarget.dataset.imageHref;
    var that = this;
    wx.downloadFile({
      url: imgUrl,
      type: 'image',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        wx.saveFile({
          tempFilePath: tempFilePath,
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            console.log(savedFilePath);
            that.setData({
              saveImage: true
            });
          },
          fail: function (res) {
            console.log(res);
          }
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
})
